<h1 class="layui-inline">订单管理</h1>
<hr class="layui-bg-black">
<!--<link rel="shortcut icon" href="../../img/favicon.ico" type="image/x-icon">-->
<table id="tb_users" class="layui-table" lay-filter="users">
</table>

<script type="text/html" id="barDemo">
    <div class="layui-btn-container">
        <a class="layui-btn layui-btn-xs" lay-event="more">状态选择<i class="layui-icon layui-icon-down"></i></a>
    </div>
</script>
<script>

    layui.use(function () {
        var $ = layui.$;
        var table = layui.table;
        var form = layui.form;
        var dropdown = layui.dropdown;
        table.render({
            elem: '#tb_users',
            url: "/main/allorder",
            id: 'idTest',
            cols: [[
                {field: 'id', width: 120, sort: true, title: '订单编号'},
                {field: 'uid', width: 120, sort: true, title: '商品编号'},
                {field: 'price', width: 120, sort: true, title: '价格'},
                {field: 'ctime', sort: true, title: '订单时间'},
                {field: 'ptime', width: 160, sort: true, title: '订单状态'},
                {field: 'state', width: 120, sort: true, title: '用户编号'},
                {fixed: 'right', width: 120, align: 'center', title: '操作', toolbar: '#barDemo'}
            ]]
        });

        //监听工具条
        table.on('tool(users)', function (obj) {
            var trData = obj.data;
            var layEvent = obj.event;
            var data = obj.data;
            if (layEvent === "more") {
                // 下拉菜单
                dropdown.render({
                    elem: this, // 触发事件的 DOM 对象
                    show: true, // 外部事件触发即显示
                    align: "right", // 右对齐弹出
                    data: [
                        {
                            title: "已发货",
                            id: "1"
                        },
                        {
                            title: "送货中",
                            id: "2"
                        },
                        {
                            title: "已送达",
                            id: "3"
                        },
                        {
                            title: "未完成",
                            id: "4"
                        }
                    ],
                    click: function (menudata) {
                        if (menudata.id === "1") {
                            layer.confirm("修改为已发货", function (index) {
                                $.ajax({
                                    url: '/main/uporder', // 替换为后端接口地址
                                    type: 'GET',
                                    data:{'ptime':'已发货',
                                    'id':trData.id},
                                    success: function (data) {
                                        // form.render();
                                        $.ajax({
                                            url: '/main/allorder', // 替换为后端接口地址
                                            type: 'GET',
                                            success: function (data) {
                                                table.reload('idTest', {
                                                    data: data // 将更新后的数据传递给表格
                                                });
                                            },
                                            error: function () {
                                                layer.msg('更新失败');
                                            }
                                        });
                                    },
                                    error: function () {
                                        layer.msg('更新失败');
                                    }
                                });
                                layer.close(index);
                            });
                        } else if (menudata.id === "2") {
                            layer.confirm("修改为送货中", function (index) {
                                $.ajax({
                                    url: '/main/uporder', // 替换为后端接口地址
                                    type: 'GET',
                                    data:{'ptime':'送货中',
                                        'id':trData.id},
                                    success: function (data) {
                                        $.ajax({
                                            url: '/main/allorder', // 替换为后端接口地址
                                            type: 'GET',
                                            success: function (data) {
                                                table.reload('idTest', {
                                                    data: data // 将更新后的数据传递给表格
                                                });
                                            },
                                            error: function () {
                                                layer.msg('更新失败');
                                            }
                                        });
                                    },
                                    error: function () {
                                        layer.msg('更新失败');
                                    }
                                });
                                layer.close(index);
                            });
                        }else if (menudata.id === "3") {
                            layer.confirm("修改为已完成", function (index) {
                                $.ajax({
                                    url: '/main/uporder', // 替换为后端接口地址
                                    type: 'GET',
                                    data:{'ptime':'已完成',
                                        'id':trData.id},
                                    success: function (data) {
                                        // form.render();
                                        $.ajax({
                                            url: '/main/allorder', // 替换为后端接口地址
                                            type: 'GET',
                                            success: function (data) {
                                                table.reload('idTest', {
                                                    data: data // 将更新后的数据传递给表格
                                                });
                                            },
                                            error: function () {
                                                layer.msg('更新失败');
                                            }
                                        });
                                    },
                                    error: function () {
                                        layer.msg('更新失败');
                                    }
                                });
                                layer.close(index);
                            });
                        }else if (menudata.id === "4") {
                            layer.confirm("修改为未完成", function (index) {
                                $.ajax({
                                    url: '/main/uporder', // 替换为后端接口地址
                                    type: 'GET',
                                    data:{'ptime':'未完成',
                                        'id':trData.id},
                                    success: function (data) {
                                        // form.render();
                                        $.ajax({
                                            url: '/main/allorder', // 替换为后端接口地址
                                            type: 'GET',
                                            success: function (data) {
                                                table.reload('idTest', {
                                                    data: data // 将更新后的数据传递给表格
                                                });
                                            },
                                            error: function () {
                                                layer.msg('更新失败');
                                            }
                                        });
                                    },
                                    error: function () {
                                        layer.msg('更新失败');
                                    }
                                });
                                layer.close(index);
                            });
                        }
                    }
                });
            }
        });
    });
</script>